{{>licenseInfo}}
package body {{package}}.Models is
   pragma Style_Checks ("-bmrIu");

   pragma Warnings (Off, "*use clause for package*");

   use {{openApiPackageName}}.Streams;

{{#orderedModels}}{{#model}}{{^isArray}}{{#isEnum}}
   function To_{{classname}} (Value : in String) return {{vendorExtensions.x-ada-type-name}} is
   begin{{#allowableValues}}{{#enumVars}}
      if Value = {{value}} then
         return {{vendorExtensions.x-ada-prefix}}{{&name}};
      end if;{{/enumVars}}{{/allowableValues}}
      raise Constraint_Error;
   end To_{{classname}};

   function To_String (Value : in {{vendorExtensions.x-ada-type-name}}) return String is
   begin
      case Value is{{#allowableValues}}{{#enumVars}}
         when {{vendorExtensions.x-ada-prefix}}{{&name}} =>
            return {{value}};
{{/enumVars}}{{/allowableValues}}
      end case;
   end To_String;
{{/isEnum}}
   procedure Serialize (Into  : in out {{openApiPackageName}}.Streams.Output_Stream'Class;
                        Name  : in String;
                        Value : in {{vendorExtensions.x-ada-type-name}}) is
   begin{{#isEnum}}
      Into.Write_Entity (Name, To_String (Value));{{/isEnum}}{{^isEnum}}
      Into.Start_Entity (Name);{{#vars}}{{#vendorExtensions.x-is-stream-type}}{{^isLong}}{{#vendorExtensions.x-is-required}}
      Into.{{vendorExtensions.x-ada-serialize-op}} ("{{baseName}}", Value.{{name}});{{/vendorExtensions.x-is-required}}{{^vendorExtensions.x-is-required}}{{#vendorExtensions.x-is-nullable}}
      Into.{{vendorExtensions.x-ada-serialize-op}} ("{{baseName}}", Value.{{name}});{{/vendorExtensions.x-is-nullable}}{{^vendorExtensions.x-is-nullable}}
      if not {{#isFreeFormObject}}{{openApiPackageName}}.Is_Null (Value.{{name}}){{/isFreeFormObject}}{{^isFreeFormObject}}Value.{{name}}.Is_Null{{/isFreeFormObject}} then
         Into.{{vendorExtensions.x-ada-serialize-op}} ("{{baseName}}", Value.{{name}});
      end if;{{/vendorExtensions.x-is-nullable}}{{/vendorExtensions.x-is-required}}{{/isLong}}{{#isLong}}{{#vendorExtensions.x-is-required}}
      Into.{{vendorExtensions.x-ada-serialize-op}} ("{{baseName}}", Value.{{name}});{{/vendorExtensions.x-is-required}}{{^vendorExtensions.x-is-required}}{{#vendorExtensions.x-is-nullable}}
      Into.{{vendorExtensions.x-ada-serialize-op}} ("{{baseName}}", Value.{{name}});{{/vendorExtensions.x-is-nullable}}{{^vendorExtensions.x-is-nullable}}
      if not Value.{{name}}.Is_Null then
         Into.{{vendorExtensions.x-ada-serialize-op}} ("{{baseName}}", Value.{{name}});
      end if;{{/vendorExtensions.x-is-nullable}}{{/vendorExtensions.x-is-required}}{{/isLong}}{{/vendorExtensions.x-is-stream-type}}{{^vendorExtensions.x-is-stream-type}}
      Serialize (Into, "{{baseName}}", Value.{{name}});{{/vendorExtensions.x-is-stream-type}}{{/vars}}
      Into.End_Entity (Name);
{{/isEnum}}
   end Serialize;
{{^vendorExtensions.x-ada-no-vector}}

   procedure Serialize (Into  : in out {{openApiPackageName}}.Streams.Output_Stream'Class;
                        Name  : in String;
                        Value : in {{#vendorExtensions.x-ada-vector-type-name}}{{.}}{{/vendorExtensions.x-ada-vector-type-name}}{{^vendorExtensions.x-ada-vector-type-name}}{{classname}}_Vectors.Vector{{/vendorExtensions.x-ada-vector-type-name}}) is
   begin
      Into.Start_Array (Name);
      for Item of Value loop
         Serialize (Into, "", Item);
      end loop;
      Into.End_Array (Name);
   end Serialize;
{{/vendorExtensions.x-ada-no-vector}}

   procedure Deserialize (From  : in {{openApiPackageName}}.Value_Type;
                          Name  : in String;
                          Value : out {{vendorExtensions.x-ada-type-name}}) is
      Object : {{openApiPackageName}}.Value_Type;
   begin
      {{openApiPackageName}}.Streams.Deserialize (From, Name, Object);{{#isEnum}}
      Value := To_{{classname}} ({{openApiPackageName}}.To_String (Object));{{/isEnum}}{{^isEnum}}{{#vars}}{{#vendorExtensions.x-is-model-type}}
      Deserialize (Object, "{{baseName}}", Value.{{name}});{{/vendorExtensions.x-is-model-type}}{{^vendorExtensions.x-is-model-type}}
      {{openApiPackageName}}.Streams.Deserialize (Object, "{{baseName}}", Value.{{name}});{{/vendorExtensions.x-is-model-type}}{{/vars}}{{/isEnum}}
   end Deserialize;
{{^vendorExtensions.x-ada-no-vector}}

   procedure Deserialize (From  : in {{openApiPackageName}}.Value_Type;
                          Name  : in String;
                          Value : in out {{#vendorExtensions.x-ada-vector-type-name}}{{.}}{{/vendorExtensions.x-ada-vector-type-name}}{{^vendorExtensions.x-ada-vector-type-name}}{{classname}}_Vectors.Vector{{/vendorExtensions.x-ada-vector-type-name}}) is
      List : {{openApiPackageName}}.Value_Array_Type;
      Item : {{vendorExtensions.x-ada-type-name}};
   begin
      Value.Clear;
      {{openApiPackageName}}.Streams.Deserialize (From, Name, List);
      for Data of List loop
         Deserialize (Data, "", Item);
         Value.Append (Item);
      end loop;
   end Deserialize;
{{/vendorExtensions.x-ada-no-vector}}
{{/isArray}}{{/model}}
{{/orderedModels}}

end {{package}}.Models;
